﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAnWeb.DAO;
using DoAnWeb.DTO;
using DoAnWeb.BUS;

namespace DoAnWeb
{
    public partial class ProductDetails : System.Web.UI.Page
    {
        TinKhuyenMaiBUS BUS = new TinKhuyenMaiBUS();
        TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["proID"] == null)
                    Response.Redirect("~/ProductByCat.aspx");

                int proID = int.Parse(Request.QueryString["proID"].ToString());
                DTO = BUS.GetFromId(proID);

                imgHinhAnh.ImageUrl = "~/images/Cat/" + DTO.HinhAnh;
                lbTenSanPham.Text = DTO.TieuDe;
                lbTenLoaiSanPham.Text = DTO.TenLoaiSanPham;
                lbGia.Text = string.Format("{0:#,#.##}", DTO.GiaBan) + " VNĐ";
                lbMoTa.Text = DTO.MoTa;

                pnDatHang.Visible = CurrentContext.IsLogged;
                upnBinhLuan.Visible = CurrentContext.IsLogged;
                LoadComment();

            }
        }

        protected void btnDatHang_Click(object sender, EventArgs e)
        {
            int SoLuong = 0;
            int.TryParse(ddlSoLuong.Text, out SoLuong);
            if (SoLuong == 0) return;
            int ProId = Convert.ToInt32(Request.QueryString["proID"]);
            CartItem item = new CartItem(ProId, SoLuong);

            //if (CurrentContext.Cart == null)
            //    CurrentContext.TaiKhoanDTO.InitCart();

            CurrentContext.Cart.AddItem(item);

            (Master as Site1).CalculateCartItems();
        }

        protected void btnGui_Click(object sender, EventArgs e)
        {
            BinhLuanDTO BinhLuan = new BinhLuanDTO();
            BinhLuan.MaTinKhuyenMai = Convert.ToInt32(Request.QueryString["proID"]);
            BinhLuan.NoiDung = txtBinhLuan.Text;
            BinhLuan.TenTaiKhoan = CurrentContext.UserName;
            new BinhLuanBUS().Add(BinhLuan);
            LoadComment();
            txtBinhLuan.Text = "";
            txtBinhLuan.Focus();
        }

        void LoadComment()
        { 
            int ProId = Convert.ToInt32(Request.QueryString["proID"]);
            dlBinhLuan.DataSource = new BinhLuanBUS().GetList_TinKhuyenMai(ProId);
            dlBinhLuan.DataBind();
        }

        protected bool VisibleDeleteComment()
        {
            if (CurrentContext.TaiKhoanDTO != null)
            {
                if (new TaiKhoanDAO().GetKind(CurrentContext.UserName) == "LTK1")
                    return true;
                return false;
            }
            return false;
        }

        protected void btnDeleteComment_Click(object sender, EventArgs e)
        {
            int MaBinhLuan = Convert.ToInt32(((Button)sender).CommandName);
            new BinhLuanBUS().CheckDelete(MaBinhLuan);
            LoadComment();
        }

        protected string GetDate()
        {
            if (Request.QueryString["proID"] != null)
            {
                int proID = int.Parse(Request.QueryString["proID"].ToString());
                TinKhuyenMaiDTO DTO = new TinKhuyenMaiBUS().GetFromId(proID);
                TimeSpan time = DTO.NgayHetHan - DateTime.Now;
                string a = time.ToString(@"dd\:hh\:mm\:ss");
                return a;
            }
            return null;
        }
    }
}